$(function () {
    initLayerDate("#beginTime",true);
    initLayerDate("#endTime",true);
    queryUserCashRecordList(1);

    $("body").on('click','#querySubmit,#submitFrom',function () {
        queryUserCashRecordList(1);
    }).on('click','.show-reject-text',function () {//显示提现审核不通过原因
        var context = $(this).next('div').html();
        icModale(1,context);
    }).on('click','.adopt-btn',function () {//通过
        var id = $(this).data('id');
        icModale(2,'通过审核后会向对方提现账户打款，确认通过？',3,function () {
            updateCashRecordStatus(id,2,null);
        });
    }).on('click','.reject-btn',function () {//驳回
        var id = $(this).data('id');
        var context = '<div class="m-t m-r m-l m-b">' +
            '驳回原因：<textarea class="form-control" id="rejectText"></textarea></div>';
        parent.layer.open({
            type: 1,
            area: ['380px'],
            skin: 'layui-layer-demo',
            title: '驳回申请',
            content:context ,
            btn: ['确定','取消'],
            yes: function (index, layero) {
                var rejectText = layero.find('#rejectText').val();
                updateCashRecordStatus(id,3,rejectText,index);
            }
        });
    });

    /**
     * 修改审核记录状态
     * @param id ID
     * @param status 状态，2通过，3驳回
     * @param rejectText 驳回内容
     */
    function updateCashRecordStatus(id,status,rejectText,index) {
        $.ajax({
            url:baseloaction +'/admin/user/account/cash/auditPass',
            type:'post',
            dataType:'json',
            data:{
                'id':id,
                'status':status,
                'rejectText':rejectText
            },
            success:function (result) {
                console.dir(result);
                icModale(0,result.message);
                if(result.success==true){
                    $("#status-"+id).text("—");
                    if(status==3){
                        $("#status-text-"+id).html('<span class="badge badge-danger vam">未通过</span><a href="javascript:void(0);" class="c-info ml5 vam show-reject-text">[原因]</a>');
                        $("#status-text-"+id).append('<div style="display: none;">'+rejectText+'</div>');
                        parent.layer.close(index);
                    }else{
                        $("#user-name-"+id).html(result.userName);
                        $("#examine-time-"+id).html(result.examineTime);
                        $("#status-text-"+id).html('<span class="badge badge-primary vam">已提现</span>');
                    }
                }
            },
            error:function (error) {
                icModale(0,'系统错误，操作失败');
            }
        });
    }

    /**
     * 加载数据列表
     * @param pageNo
     */
    function queryUserCashRecordList(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/user/account/cash/ajaQueryUserCashRecordList',//请求URL
            contextId:'user-cash-record-list-context',//响应内容页面设置内容ID
            params:params,//请求参数
            pageNo:pageNo,//页码
            pageCall:queryUserCashRecordList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            callback:function () {//数据加载完成回调

            }
        };
        ajaxGetListData(obj)
    }
});